package stark.app.function.novelreader;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.stark.novelreader.ReaderEntry;
import com.stark.novelreader.book.utils.media.MediaStoreHelper;

import java.io.File;

import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.app.R;
import stark.app.databinding.ActivityNovelReaderDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.UriUtil;

/**
 * Author：daizhongbin on 2021/6/9
 * Email：1027154338@qq.com
 * Desc:
 */
public class NovelReaderDemoActivity extends BaseNoModelActivity<ActivityNovelReaderDemoBinding> {

    private static final String TAG = NovelReaderDemoActivity.class.getSimpleName();
    private ActivityResultLauncher<Intent> mSelTxtFileLauncher;

    @Override
    protected int onCreate() {
        return R.layout.activity_novel_reader_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnLocal.setOnClickListener(v -> {
            handleClickLocal();
        });
        mDataBinding.btnLocal1.setOnClickListener(v -> {
            handleClickLocal1();
        });
    }

    @Override
    protected void initData() {
        mSelTxtFileLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() != RESULT_OK) {
                return;
            }
            Intent data = result.getData();
            Uri uri = data.getData();
            Log.e(TAG, "uri = " + uri.toString());
            showDialog("加载中...");
            RxUtil.create(new RxUtil.Callback<File>() {
                @Override
                public void doBackground(ObservableEmitter<File> emitter) {
                    File file = UriUtil.uri2File(uri);
                    emitter.onNext(file);
                }

                @Override
                public void accept(File file) {
                    dismissDialog();
                    if (file != null) {
                        ReaderEntry.readFile(NovelReaderDemoActivity.this, file);
                    }
                }
            });

        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSelTxtFileLauncher.unregister();
    }

    private void handleClickLocal() {
        StkPermissionHelper.reqManageExternalStoragePermission()
                .reqPermissionDesc("读取文件需要存储权限，请授予")
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startActivity(LocalNovelActivity.class);
                    }
                })
                .request();
    }

    private void handleClickLocal1() {
        StkPermissionHelper.reqManageExternalStoragePermission()
                .reqPermissionDesc("读取文件需要存储权限，请授予")
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                        intent.addCategory(Intent.CATEGORY_OPENABLE);
                        intent.setType("text/plain");
                        mSelTxtFileLauncher.launch(intent);
                    }
                })
                .request();
    }

}
